﻿function init() {
	if (window.DeviceMotionEvent) {
		// 移动浏览器支持运动传感事件
		window.addEventListener('devicemotion', deviceMotionHandler, false);
	}
}

var SHAKE_THRESHOLD = 3000;
// 定义一个变量保存上次更新的时间
var last_update = 0;
// 紧接着定义x、y、z记录三个轴的数据以及上一次出发的时间
var x;
var y;
var z;
var last_x;
var last_y;
var last_z;
var count = 0;
var flag = true;

function deviceMotionHandler(eventData) {
	// 获取含重力的加速度
	var acceleration = eventData.accelerationIncludingGravity;
	// 获取当前时间
	var curTime = new Date().getTime();
	var diffTime = curTime - last_update;
	// 固定时间段
	if (diffTime > 100) {
		last_update = curTime;

		x = acceleration.x;
		y = acceleration.y;
		z = acceleration.z;

		var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 30000;

		if (speed > SHAKE_THRESHOLD) {
			// TODO:在此处可以实现摇一摇之后所要进行的数据逻辑操作
			count++;
			var px = count * 5;
			var text = count * 8;
			var number = 124 - px;
			if(number > -20){
				var gamer = document.getElementById("game-start-water");
				var gamertext = document.getElementById("shaketext01");
				gamertext.style.cssText = "display:none;";
				gamer.style.cssText = "position:absolute; z-index:1; top:"+ (number) + "px;";
//				$("#shaketext").html("DK/t" + text);
				if (px >= 140 && flag) {
					flag = false;
					alert("谢谢您的参与，下次再加油哦！！");
					save(diffTime);
				}
			}
		}

		last_x = x;
		last_y = y;
		last_z = z;
	}
}
